package com.octopus.tree.imp.simple;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.octopus.string.StringUtils;
import com.octopus.tree.TreeBranch;
import com.octopus.tree.Unit;

public class SimpleUtils {

	public static String convertClassNameToDefaultLikeXpath(String className){
		if(className == null){
			return null;
		}
		StringUtils.StringContainer sc = new StringUtils.StringContainer(className);
		if(StringUtils.isNullString(sc)){
			return null;
		}
		return StringUtils.concat("/",sc.getAfterTrimedString().replaceAll("\\.", "/"), "[kind=$default]");
	}
	
	/*
	 * Currently we support:
	 * name,
	 * kind,
	 * index,
	 * properties
	 */
	public static List<Unit> parseLikeXpath(String path) {
		List<Unit> result = new ArrayList<Unit>();
		if (StringUtils.isNullString(path)) {
			return result;
		}
		path = path.trim();
		String[] unitStringArray = path.split("/");
		for (String unitString : unitStringArray) {
			Unit unit = new Unit();
			if (StringUtils.isNullString(unitString)) {
				continue;
			}
			unitString = unitString.trim();
			int indexLeft = unitString.indexOf("[");
			int indexRight = unitString.indexOf("]");
			if (indexLeft < 0 || (indexLeft > indexRight)) {
				if(!unitString.matches("(\\*)|([a-zA-Z0-9_-]+)")){
					continue;
				}
				unit.setName(unitString);
				result.add(unit);
				continue;
			}
			String name = unitString.substring(0, indexLeft).trim();
			if(!name.matches("(\\*)|([a-zA-Z0-9_-]+)")){
				continue;
			}
			unit.setName(name);
			String others = "";
			if (indexRight > indexLeft) {
				others = unitString.substring(indexLeft + 1, indexRight);
			}
			others = others.trim();
			String[] otherStringArray = others.split(",");
			for (String otherString : otherStringArray) {
				if (otherString.matches("\\s*kind\\s*\\=\\s*((\\$default)|([a-zA-Z0-9_-]+))\\s*")) {
					int index = otherString.indexOf("=");
					String kind = otherString.substring(index + 1).trim();
					unit.setKind(kind);
					continue;
				}
				
				if (otherString
						.matches("\\s*index\\s*\\=\\s*[0-9]+(\\-(([0-9]+)|\\*))?\\s*")) {
					int index = otherString.indexOf("=");
					String indices = otherString.substring(index + 1).trim();
					int dash_index = indices.indexOf("-");
					if(dash_index < 0){
						int indexValue = Integer.parseInt(indices);
						unit.setStartIndex(indexValue);
						unit.setEndIndex(indexValue);
					}else{
						String startIndexString = indices.substring(0, dash_index).trim();
						String endIndexString = indices.substring(dash_index+1).trim();
						unit.setStartIndex(Integer.parseInt(startIndexString));
						if("*".equals(endIndexString)){
							unit.setEndIndex(Integer.MAX_VALUE);
						}else{
							unit.setEndIndex(Integer.parseInt(endIndexString));
						}
					}
					continue;
				}
				
				if (otherString
						.matches("\\s*\\{[a-zA-Z0-9-_]+\\s*\\:\\s*[.a-zA-Z0-9-_]+\\}\\s*")) {
					otherString = otherString.replace('{', ' ');
					otherString = otherString.replace('}', ' ');
					int index = otherString.indexOf(":");
					String nameKey = otherString.substring(0, index).trim();
					String value = otherString.substring(index+1).trim();
					unit.addProperty(nameKey, value);
					continue;
				}
				
			}
			if(unit.getName() == null && unit.getKind() == null && unit.propertySize() == 0){
				continue;
			}else{
				result.add(unit);
			}
		}
		return result;
	}
	
	public static boolean isRootBranch(TreeBranch tb){
		return tb.getTreeBranchProperty(SimpleImpConstants.SYSTEM_ROOT) != null;
	}

	/**
	 * @param args
	 */
	public static void main(String args[]){
		/*
		String test = "/name[kind=$default, index=0-9, {kk:vv}, {ww:tt}]/b[kind=kind_of_node, {k:l.t}, {v:o}]/c/c/d";
		List<Unit> result = parseLikeXpath(test);
		System.out.println(result);
		*/
		
		System.out.println(convertClassNameToDefaultLikeXpath(SimpleUtils.class.getName()));
	}
}
